#!/bin/bash

# ============================================
# Worker Setup Script (macOS/Linux)
# ============================================

set -e  # Exit on error

# Get script directory and change to project root
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR/../.."

echo "=================================="
echo "  Worker Setup Script"
echo "=================================="
echo ""

# Color definitions
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# Check Python version
echo -e "${YELLOW}[1/5] Checking Python...${NC}"
if ! command -v python3 &> /dev/null; then
    echo -e "${RED}[ERROR] python3 not found. Please install Python 3.8+${NC}"
    exit 1
fi

PYTHON_VERSION=$(python3 --version | cut -d' ' -f2 | cut -d'.' -f1,2)
echo -e "${GREEN}[OK] Found Python $PYTHON_VERSION${NC}"

# Check pip
echo ""
echo -e "${YELLOW}[2/5] Checking pip...${NC}"
if ! command -v pip3 &> /dev/null; then
    echo -e "${RED}[ERROR] pip3 not found${NC}"
    exit 1
fi
echo -e "${GREEN}[OK] pip3 installed${NC}"

# Ask about virtual environment
echo ""
echo -e "${YELLOW}[3/5] Installing dependencies...${NC}"
read -p "Use virtual environment? (recommended) [Y/n]: " use_venv
use_venv=${use_venv:-Y}

if [[ $use_venv =~ ^[Yy]$ ]]; then
    if [ ! -d "venv" ]; then
        echo "Creating virtual environment..."
        python3 -m venv venv
    fi
    echo "Activating virtual environment..."
    source venv/bin/activate
    echo -e "${GREEN}[OK] Virtual environment activated${NC}"
fi

# Install dependencies
echo "Installing Python packages (this may take a few minutes)..."
pip3 install -r requirements.txt --upgrade

echo -e "${GREEN}[OK] Dependencies installed${NC}"

# Create directories
echo ""
echo -e "${YELLOW}[4/5] Creating directories...${NC}"
mkdir -p logs cache output
echo -e "${GREEN}[OK] Directories created${NC}"

# Check config files
echo ""
echo -e "${YELLOW}[5/5] Checking config files...${NC}"
if [ ! -f "config/worker_config.json" ]; then
    if [ -f "config/worker_config.example.json" ]; then
        cp config/worker_config.example.json config/worker_config.json
        echo -e "${YELLOW}[WARN] Created config file: config/worker_config.json${NC}"
        echo -e "${YELLOW}       Please run: ./scripts/unix/config_wizard.sh${NC}"
    else
        echo -e "${RED}[ERROR] Example config file not found${NC}"
        exit 1
    fi
else
    echo -e "${GREEN}[OK] Config file exists${NC}"
fi

if [ ! -f ".env" ]; then
    if [ -f "config/.env.example" ]; then
        cp config/.env.example .env
        echo -e "${YELLOW}[WARN] Created .env file${NC}"
        echo -e "${YELLOW}       For AI polish, add DeepSeek API Key to .env${NC}"
    fi
else
    echo -e "${GREEN}[OK] .env file exists${NC}"
fi

# Complete
echo ""
echo -e "${GREEN}=================================="
echo -e "  Setup Complete!"
echo -e "==================================${NC}"
echo ""
echo "Next steps:"
echo ""
echo "  1. Configure Worker (first time):"
echo "     ./scripts/unix/config_wizard.sh"
echo ""
echo "  2. Start Worker:"
echo "     ./scripts/unix/start_worker.sh --excel /path/to/excel.xlsx"
echo ""
echo "  3. View help:"
echo "     ./scripts/unix/start_worker.sh --help"
echo ""

if [[ $use_venv =~ ^[Yy]$ ]]; then
    echo -e "${YELLOW}Note: After closing terminal, activate venv first:${NC}"
    echo "     source venv/bin/activate"
    echo ""
fi
